/*
 * JMEDesktopTest.java
 *
 * Created on Jul 8, 2007, 3:37:03 PM
 *
 * Copyright (c) 2007 Daniel Gronau
 *
 * This file is part of KanjiTori.
 *
 * KanjiTori is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * KanjiTori is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

package kanjitori;

import com.jme.renderer.ColorRGBA;
import com.jme.system.GameSettings;
import com.jme.system.PreferencesGameSettings;
import com.jme.util.GameTaskQueueManager;
import com.jmex.awt.swingui.JMEDesktopState;
import com.jmex.game.StandardGame;
import com.jmex.game.state.GameState;
import com.jmex.game.state.GameStateManager;
import java.io.File;
import java.util.concurrent.Callable;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import javax.swing.UIManager;
import kanjitori.gamestate.BackgroundGameState;
import kanjitori.gamestate.InputMenuGameState;
import kanjitori.gamestate.MenuGameState;
import kanjitori.gamestate.VideoMenuGameState;
import kanjitori.preferences.KeyMap;
import org.ini4j.IniFile;

/**
 *
 * @author DGronau
 */
public class KanjiTori {
    
    private static StandardGame game;
    private static GameSettings gameSettings;
    private static Preferences preferences;
    
    private KanjiTori() {
    }

    public static void main(String[] args) throws Exception {
        UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
        com.birosoft.liquid.LiquidLookAndFeel.setLiquidDecorations(true); 

        preferences = new IniFile(new File("kanjitori.ini"), IniFile.Mode.RW).node("kanjitori");
        
        gameSettings = new PreferencesGameSettings(preferences);
        KeyMap.init(gameSettings);
        
        game = new StandardGame("KanjiTori", StandardGame.GameType.GRAPHICAL,
                gameSettings);
        game.setBackgroundColor(new ColorRGBA(0.1f, 0.5f, 0.4f, 0f));
        game.start();
        setMenuGameState();
   }
    
    private static void setState(GameState gameState) {
         GameStateManager.getInstance().attachChild(gameState);
         gameState.setActive(true); 
    }
    
    private static void clearStates() {
        GameStateManager.getInstance().deactivateAllChildren();
        GameStateManager.getInstance().detachAllChildren();
    }
    
    public static void shutdown() {
        game.shutdown();
    }

    public static void setMenuGameState() {
        clearStates();
        setState(new BackgroundGameState("data/menu/menuMain.png"));
        setState(new MenuGameState());
    }
    
    public static void setVideoGameState() {
        GameTaskQueueManager.getManager().render(new Callable<Object>() {

            public Object call() throws Exception {
        clearStates();
        setState(new BackgroundGameState("data/menu/menuVideo.png"));
        setState(new VideoMenuGameState(gameSettings));
        return null;
            }
        });
    }
    
    public static void setInputGameState() {
        clearStates();
        setState(new BackgroundGameState("data/menu/menuInput.png"));
        setState(new InputMenuGameState(gameSettings));
    }
    
    public static void savePreferences() {
        try {
            preferences.flush();
        } catch (BackingStoreException ex) {
            Logger.getLogger("global").log(Level.SEVERE, null, ex);
        }
    }
    
    public static void changeResolution() {
        game.recreateGraphicalContext();
    }

}
